# -*- coding: utf-8 -*-
"""Main Controller"""

from tg import expose, flash, require, url, request, redirect
from pylons.i18n import ugettext as _, lazy_ugettext as l_

from crv.lib.base import BaseController
from crv.model import DBSession, metadata
from crv.controllers.error import ErrorController
from crv import model

import logging
log = logging.getLogger(__name__)

__all__ = ['RootController']

from crvcontroller import *

class RootController(BaseController):
    """
    The root controller for the CRV application.
    
    All the other controllers and WSGI applications should be mounted on this
    controller. For example::
    
        panel = ControlPanelController()
        another_app = AnotherWSGIApplication()
    
    Keep in mind that WSGI applications shouldn't be mounted directly: They
    must be wrapped around with :class:`tg.controllers.WSGIAppController`.
    
    """

    import inspect
    from sqlalchemy.orm import class_mapper
    
    models = {}
    for m in dir(model):
        m = getattr(model, m)
        if not inspect.isclass(m):
            continue
        try:
            mapper = class_mapper(m)
            models[m.__name__.lower()] = m
        except:
            pass

    log.debug("Model=(%s)"%str(models))
                                            
    clusters = ClusterController(DBSession)
    roles = RoleController(DBSession)
    vms = VMController(DBSession)
    roledefs = RoleDefController(DBSession)

    clusterattrs = ClusterAttrController(DBSession)
    roleattrs = RoleAttrController(DBSession)
    vmattrs = VMAttrController(DBSession)    

    status=StatusController()
    
    error = ErrorController()

    @expose('crv.templates.index')
    def index(self):
        """Handle the front-page."""
        return dict(page='index')

    @expose('crv.templates.about')
    def about(self):
        """Handle the 'about' page."""
        return dict(page='about')


